<template>
  <div class="body">
    <div class="table-wrap">
      <div class="search_header">
        <div></div>
        <!-- <el-button  icon="el-icon-plus" @click="dialogVisible=true,headerTitle='新增用户'">新增用户</el-button> -->
        <div class="search_header_right">
          <el-input
            placeholder="请输入用户真实姓名"
            v-model="parameter.userName"
            clearable
            @input="search"
          >
            <div slot="suffix">
              <Icon
                title="搜索"
                name="sousuo"
                color="#A7ADB5"
                font-size="20"
              />
            </div>
          </el-input>
          <el-select
            placeholder="用户角色"
            v-model="parameter.roleId"
            clearable
            @change="search"
          >
            <el-option
              v-for="item in roleOptions"
              :key="item.pkid"
              :label="item.roleName"
              :value="item.pkid"
            >
            </el-option>
          </el-select>
          <el-cascader
            placeholder="选择部门"
            clearable
            @change="search"
            v-model="parameter.deptId"
            :options="deptTree"
            :props="{
              value: 'pkid',
              label: 'deptName',
              children: 'childTree',
              checkStrictly: true
            }"
          />
          <!-- <el-select
            placeholder="用户状态"
            v-model="parameter.userState"
            clearable
            @change="search"
          >
            <el-option
              v-for="item in stateOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select> -->
        </div>
      </div>
      <el-table
        :data="userList"
        :header-cell-style="{
          background: '#F2F6FB ',
          color: ' #6B767D'
        }"
        :row-class-name="tableRowClassName"
      >
        <el-table-column
          label="序号"
          align="center"
          show-overflow-tooltip
          width="80"
        >
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column
          label="用户名"
          prop="loginName"
          show-overflow-tooltip
        />
        <el-table-column
          label="真实姓名"
          prop="userName"
          show-overflow-tooltip
        />
        <el-table-column
          label="用户角色"
          prop="roleName"
          show-overflow-tooltip
        />
        <el-table-column
          label="所属单位"
          prop="deptName"
          show-overflow-tooltip
        />
        <el-table-column
          label="最后登录时间"
          prop="lastLoginTime"
          show-overflow-tooltip
        />
        <el-table-column
          label="登录次数"
          prop="loginTimes"
        />
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button
              type="text"
              @click="goDetail(scope.row)"
              style="color: #00baff; font-weight: 400"
              >登陆日志</el-button
            >
            <el-button
              type="text"
              @click="goBehavior(scope.row)"
              style="color: #00baff; font-weight: 400"
              >操作日志</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="table-pagination" v-if="total">
        <el-pagination
          background
          layout="prev, pager, next"
          :page-size="parameter.pageSize"
          @current-change="pageChange"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { userQuery } from '@/api/user'
import { roleQuery } from '@/api/role'
import { getDeptTree } from '@/api/dept'
import Icon from '@/components/icon'
export default {
  name: 'index',
  components: {
    Icon
  },
  data() {
    return {
      userList: [],
      total: '',
      headerTitle: '新增用户',
      pkid: '',
      parameter: {
        sortField: '',
        pageSize: 10,
        pageIndex: 1,
        sortType: '',
        userName: '',
        userState: '',
        deptId: '',
        roleId: ''
      },
      roleOptions: [
        // {
        //   label: '全部',
        //   value: ''
        // },
        // {
        //   label: '普通用户',
        //   value: 'common'
        // }
      ],
      deptTree: [],
      forbidden: false,
      start: true,
      dialogVisible: false
    }
  },
  watch: {
    start(value) {
      if (value) {
        this.forbidden = false
        this.addUser.userstate = 1
      }
    },
    forbidden(value) {
      if (value) {
        this.start = false
        this.addUser.userstate = 0
      }
    }
  },
  mounted() {
    roleQuery({
      pageSize: 9999,
      pageIndex: 1
    }).then((res) => {
      if (res.success) {
        this.roleOptions = res.data.list
        console.log(this.roleOptions, 'roleOptions')
      }
    })
    this.getList()
    getDeptTree().then((res) => {
      if (res.success) {
        this.deptTree = this.deptScreen(res.data)
      } else {
        this.$message.error(res.message)
      }
    })
  },
  methods: {
    search() {
      this.parameter.pageIndex = 1
      this.total = ''
      if (Array.isArray(this.parameter.deptId)) {
        this.parameter.deptId =
          this.parameter.deptId[
            this.parameter.deptId.length - 1
          ]
      }
      this.getList()
    },
    goDetail(e) {
      this.$router.push({
        path: '/mochaITOM/userDetail',
        query: { data: JSON.stringify(e) }
      })
    },
    goBehavior(e) {
      this.$router.push({
        path: '/mochaITOM/userOperation',
        query: { data: JSON.stringify(e) }
      })
    },
    // pageChange
    pageChange(page) {
      this.parameter.pageIndex = page
      this.getList()
    },
    // 获取用户列表
    getList() {
      userQuery(this.parameter).then((res) => {
        if (res.success) {
          this.userList = res.data.list
          this.total = res.data.count
        } else {
          this.$message.error(res.message)
        }
      })
    },
    // tanble row
    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 === 1) {
        return 'light'
      } else {
        return ''
      }
    },
    // 部门树筛选
    deptScreen(data) {
      data.map((item) => {
        if (item.childTree.length < 1) {
          item.childTree = undefined
        } else {
          this.deptScreen(item.childTree)
        }
      })
      return data
    }
  }
}
</script>

<style scoped lang="scss">
@import '~@/assets/css/table.scss';
</style>
